package com.hcbd.tdwt.ui.xingyu.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hcbd.tdwt.R;


/**
 * - @Description:  收到旧协议弹框
 * - @Author:  LXJ
 * - @Time:  2019/5/23 17:41
 */

public class OldProtocolDialog extends DialogFragment implements View.OnClickListener {
    private Activity mContext;
    private String content;

    private OnCloseListener listener;
    public OldProtocolDialog() {

    }

    public void setData(Activity context, String content, OnCloseListener onCloseListener) {
        this.mContext = context;
        this.content = content;
        this.listener=onCloseListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        setCancelable(false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewContent = inflater.inflate(R.layout.dialog_old_protoclo, container, false);
        //去掉dialog默认标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        //警告标题
        TextView warnTv = viewContent.findViewById(R.id.warn_dialog_tv);
        warnTv.setText(content);
        //取消按钮
        Button cancelBtn = viewContent.findViewById(R.id.warn_has_cancel_btn);
        cancelBtn.setOnClickListener(view -> dismiss());
        Button confirmBtn = viewContent.findViewById(R.id.warn_confirm_btn);
        confirmBtn.setOnClickListener(this);

        return viewContent;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.warn_confirm_btn:
                if(listener != null){
                    listener.confirm(this,true);
                }
                break;
        }
    }

    public interface OnCloseListener {
        void confirm(DialogFragment dialog, boolean confirm);
    }
}
